#include "view_project_dialog.h"
#include <QFileInfo>
#include <QDir>
#include <QTableWidgetItem>
#include <QPushButton>
#include <QDesktopServices>
#include <QUrl>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QMessageBox>

ViewProjectDialog::ViewProjectDialog(QWidget *parent) : QDialog(parent)
{
    ui.setupUi(this);
    loadStudents();
    connect(ui.btnBack, &QPushButton::clicked, this, &QDialog::accept);
}

void ViewProjectDialog::loadStudents()
{
    ui.tableWidget->setRowCount(0);
    QFile file("students.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
    QTextStream in(&file);
    int row = 0;
    while (!in.atEnd()) {
        QString line = in.readLine().trimmed();
        if (line.isEmpty()) continue;
        QStringList parts = line.split(",");
        if (parts.size() >= 1) {
            QString name = parts[0].trimmed();
            QString clazz = parts.size() >= 2 ? parts[1].trimmed() : "";
            // 账号推断：姓名拼音+编号，假设users.txt顺序与students.txt一致
            QString account;
            QFile ufile("users.txt");
            if (ufile.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QTextStream uin(&ufile);
                int idx = 0;
                while (!uin.atEnd()) {
                    QString uline = uin.readLine().trimmed();
                    if (uline.isEmpty()) continue;
                    if (idx == row+5) { // 跳过前5个非学生账号
                        QStringList uparts = uline.split(",");
                        if (uparts.size() >= 1) account = uparts[0].trimmed();
                        break;
                    }
                    ++idx;
                }
            }
            QString submitStatus = "未提交";
            bool hasFile = false;
            if (!account.isEmpty()) {
                QDir dir(QDir::currentPath());
                QStringList files = dir.entryList(QStringList() << account + ".*", QDir::Files);
                if (!files.isEmpty()) {
                    submitStatus = "已提交";
                    hasFile = true;
                }
            }
            ui.tableWidget->insertRow(row);
            ui.tableWidget->setItem(row, 0, new QTableWidgetItem(name));
            ui.tableWidget->setItem(row, 1, new QTableWidgetItem(clazz));
            ui.tableWidget->setItem(row, 2, new QTableWidgetItem(submitStatus));
            QPushButton *btnView = new QPushButton("查看");
            btnView->setEnabled(hasFile);
            ui.tableWidget->setCellWidget(row, 3, btnView);
            QPushButton *btnDownload = new QPushButton("下载");
            btnDownload->setEnabled(hasFile);
            ui.tableWidget->setCellWidget(row, 4, btnDownload);
            if (hasFile) {
                QString fileName;
                QDir dir(QDir::currentPath());
                QStringList files = dir.entryList(QStringList() << account + ".*", QDir::Files);
                if (!files.isEmpty()) fileName = files.first();
                QObject::connect(btnView, &QPushButton::clicked, this, [fileName]() {
                    QDesktopServices::openUrl(QUrl::fromLocalFile(QDir::currentPath() + "/" + fileName));
                });
                QObject::connect(btnDownload, &QPushButton::clicked, this, [fileName, this]() {
                    QString destDir = QFileDialog::getExistingDirectory(this, "选择下载文件夹");
                    if (!destDir.isEmpty()) {
                        QString srcPath = QDir::currentPath() + "/" + fileName;
                        QString destPath = destDir + "/" + fileName;
                        if (QFile::copy(srcPath, destPath)) {
                            QMessageBox::information(this, "下载成功", "文件已成功下载到：" + destPath);
                        } else {
                            QMessageBox::warning(this, "下载失败", "文件下载失败，可能目标文件已存在或无权限。");
                        }
                    }
                });
            }
            row++;
        }
    }
} 